package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl  implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断要加入到购物车的商品是否已经在购物车中
        ShoppingCart cart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        Long userId= BaseContext.getCurrentId();
        cart.setUserId(userId);

        List<ShoppingCart> list=shoppingCartMapper.list(cart);
        if(list!=null&&list.size()>0){
            //如果已经在购物车中，则数量加1
            cart=list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //如果没有在购物车中，则直接加入购物车
            //判断是菜品还是套餐
            Long dishId=shoppingCartDTO.getDishId();
            Long setmealId=shoppingCartDTO.getSetmealId();
            if(dishId!=null){
                Dish dish=dishMapper.selectById(dishId);
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());
            } else if (setmealId!=null) {
                //套餐
                Setmeal setmeal=setmealMapper.selectById(setmealId);
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setAmount(setmeal.getPrice());
            }
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.add(cart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        Long userId= BaseContext.getCurrentId();
        ShoppingCart cart=new ShoppingCart();
        cart.setUserId(userId);
        return shoppingCartMapper.list(cart);
    }

    @Override
    public void clear() {
        Long userId= BaseContext.getCurrentId();
        shoppingCartMapper.clearByUserId(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //判断要减少的商品的数量是否大于1
        ShoppingCart cart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        Long userId= BaseContext.getCurrentId();
        cart.setUserId(userId);

        List<ShoppingCart> list=shoppingCartMapper.list(cart);
        if(list!=null&&list.size()>0){
            //如果数量大于1，则数量减1
            cart=list.get(0);
            if(cart.getNumber()>1){
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.updateNumberById(cart);
            }else {
                //如果数量等于1，则直接删除
                shoppingCartMapper.deleteById(cart.getId());
            }
        }
    }
}
